package com.czd.support.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.czd.support.model.FeedBack;
import com.czd.support.service.FeedBackService;


@Controller
@RequestMapping(value = "/feedback")
public class FeedBackController extends BaseController {
	
	@Autowired
	private FeedBackService feedBackService;
	
	
	@RequestMapping(value = {"","/","list"},method = RequestMethod.GET)
    public String list(Model model,FeedBack feedBack,@RequestParam(value = "pageNo",defaultValue = "1") int pageNo){
		feedBack.setPageNo(pageNo);
        model.addAttribute("paginate",feedBackService.queryForPaginate(feedBack));
        return "feedback/list";
    }
	
	@RequestMapping(value="/addcontent" , method = RequestMethod.POST)
	public String save(FeedBack feedBack,HttpServletRequest request,Model model){
		if(StringUtils.isBlank(feedBack.getWay())){
			return  error(model, "操作提示", "信息类别不许为空");
		}
		if(StringUtils.isBlank(feedBack.getCotent())){
			return  error(model, "操作提示", "内容不许为空");
		}
		if(feedBackService.insert(feedBack)){
			return "redirect:/feedback/list";
		}
		return  error(model, "操作提示", "系统设置内容录入失败");
	}
	
	@RequestMapping(value = "/add",method = RequestMethod.GET)
    public String add(){
        return "feedback/add";
    }
	
	@RequestMapping(value = "/add",method = RequestMethod.POST)
    public String save() {
       
        return "feedback/add";
    }
	
	@RequestMapping(value = "/{id}/delete",method = RequestMethod.GET)
	public String delete(@PathVariable int id,FeedBack feedBack,Model model){
		System.out.println(feedBack);
		if (feedBackService.delete(feedBack)) {
			return "redirect:/feedback/list";
		} else {
			return error(model, "操作失败", "删除的信息内容已经不存在");
		}
	}
	
	@RequestMapping(value = "/{id}/details",method = RequestMethod.GET)
    public String view(@PathVariable int id,Model model){
		FeedBack feedBack = feedBackService.findById(id);
        if(feedBack != null){
            model.addAttribute("feedBack",feedBack);
            return "feedback/details";
        }
        return error(model,"信息不存在","信息内容不存在");
    }
	
	@RequestMapping(value = "/{id}/edit",method = RequestMethod.GET)
    public String change(@PathVariable int id,Model model){
		FeedBack feedBack = feedBackService.findById(id);
        if(feedBack != null){
            model.addAttribute("feedBack",feedBack);
            return "feedback/edit";
        }
        return error(model,"信息不存在","内容不存在");
    }
	
	@RequestMapping(value = "/{id}/edit",method = RequestMethod.POST)
    public String update(@PathVariable int id,FeedBack feedBack,HttpServletRequest request,Model model){
	//	Manager manager = this.getSesionUser(request);
		System.out.println(feedBack);
		if (feedBackService.update(feedBack)) {
			return "redirect:/feedback/list";
		} else {
			return error(model, "操作失败", "信息内容已经不存在");
		}
	
	   }

}
